package com.wjs.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wjs.gulimall.gulimallproduct.VO.AttrGroupRelationVo;
import com.wjs.gulimall.gulimallproduct.VO.AttrGroupWithAttrVo;
import com.wjs.gulimall.gulimallproduct.entity.AttrEntity;
import com.wjs.gulimall.gulimallproduct.service.AttrAttrgroupRelationService;
import com.wjs.gulimall.gulimallproduct.service.AttrService;
import com.wjs.gulimall.gulimallproduct.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wjs.gulimall.gulimallproduct.entity.AttrGroupEntity;
import com.wjs.gulimall.gulimallproduct.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author wangjingsong
 * @email wangjingsong@gmail.com
 * @date 2022-04-17 15:11:00
 */
@RestController
@RequestMapping("gulimallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

//    /product/attrgroup/attr/relation
    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getGroupWithAttr(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupWithAttrVo> data=attrGroupService.getGroupWithAttr(catelogId);
        return R.ok().put("data",data);
    }
    @PostMapping("/attr/relation")
    public R AddRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrAttrgroupRelationService.saveBatchRelation(vos);
        return R.ok();

    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R listAttrRelation(@PathVariable("attrgroupId")Long id){
        List<AttrEntity> list =attrService.getRelationList(id);
        return R.ok().put("data",list);
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R listNotAttrRelation(@PathVariable("attrgroupId")Long id,
                                 @RequestParam Map<String, Object> params){
        PageUtils page=attrService.getNoRelationList(params,id);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath=categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
